package com.neuedu.cockpit.service.impl;

import java.util.Date;
import java.util.List;

import com.neuedu.cockpit.common.Encode;
import com.neuedu.cockpit.domain.HmFeedback;
import com.neuedu.cockpit.mapper.HmFeedbackMapper;
import com.neuedu.cockpit.mapper.HmFilePathMapper;
import com.neuedu.cockpit.service.IHmFeedbackService;
import com.neuedu.common.core.domain.entity.SysUser;
import com.neuedu.common.core.domain.model.LoginUser;
import com.neuedu.common.utils.DateUtils;
import com.neuedu.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.neuedu.common.utils.SecurityUtils.getLoginUser;

/**
 * 意见反馈Service业务层处理
 * 
 * @author neuedu
 * @date 2024-04-25
 */
@Service
public class HmFeedbackServiceImpl implements IHmFeedbackService
{
    @Autowired
    private HmFeedbackMapper hmFeedbackMapper;
    @Autowired
    private HmFilePathMapper hmFilePathMapper;
    /**
     * 查询意见反馈
     * 
     * @param id 意见反馈主键
     * @return 意见反馈
     */
    @Override
    public HmFeedback selectHmFeedbackById(Long id)
    {

        return hmFeedbackMapper.selectHmFeedbackById(id);
    }
    /**
     * 查询意见反馈列表
     * 
     * @param hmFeedback 意见反馈
     * @return 意见反馈
     */
    @Override
    public List<HmFeedback> selectHmFeedbackList(HmFeedback hmFeedback)
    {
        hmFeedback.setContent(Encode.encodeStr(hmFeedback.getContent()));
        return hmFeedbackMapper.selectHmFeedbackList(hmFeedback);
    }

    @Override
    public List<HmFeedback> selectHmFeedbackListByUserId(Long userId) {
        HmFeedback hmFeedback = new HmFeedback();
        hmFeedback.setUserId(userId);
        return hmFeedbackMapper.selectHmFeedbackList(hmFeedback);
    }

    /**
     * 新增意见反馈
     * 
     * @param hmFeedback 意见反馈
     * @return 结果
     */
    @Override
    public int insertHmFeedback(HmFeedback hmFeedback)
    {
        LoginUser user = getLoginUser();
        hmFeedback.setStatus("0");// 未读
        hmFeedback.setUserId(user.getUserId());
        hmFeedback.setCreateTime(DateUtils.getNowDate());
        hmFeedback.setCreateBy(user.getUsername());
        return hmFeedbackMapper.insertHmFeedback(hmFeedback);
    }

    /**
     * 修改意见反馈
     * 
     * @param hmFeedback 意见反馈
     * @return 结果
     */
    @Override
    public int updateHmFeedback(HmFeedback hmFeedback)
    {   LoginUser loginUser = getLoginUser();
        SysUser user = loginUser.getUser();
        hmFeedback.setStatus("1");// 已读
        hmFeedback.setUpdateBy(user.getUserName());
        hmFeedback.setUpdateTime(DateUtils.getNowDate());
        // 初始状态的时候
        if("0".equals(hmFeedback.getReplyRead())) {
            if(!StringUtils.isEmpty(hmFeedback.getReply())) {
                // 0:初始，1:回复未读，2:回复已读
                hmFeedback.setReplyRead("1");
            }
        }
        return hmFeedbackMapper.updateHmFeedback(hmFeedback);
    }

    @Override
    public int updateReplyRead(HmFeedback hm) {
        return hmFeedbackMapper.updateHmFeedback(hm);
    }

    @Override
    public int updateHmFeedbackStatus(HmFeedback hmFeedback) {

        LoginUser loginUser = getLoginUser();
        SysUser user = loginUser.getUser();
        hmFeedback.setStatus("1");
        hmFeedback.setUpdateBy(user.getUserName());
        hmFeedback.setUpdateTime(new Date());
        return hmFeedbackMapper.updateHmFeedbackStatus(hmFeedback);
    }

    /**
     * 批量删除意见反馈
     * 
     * @param ids 需要删除的意见反馈主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHmFeedbackByIds(Long[] ids)
    {
        return hmFeedbackMapper.deleteHmFeedbackByIds(ids);
    }

    /**
     * 删除意见反馈信息
     * 
     * @param id 意见反馈主键
     * @return 结果
     */
    @Override
    public int deleteHmFeedbackById(Long id)
    {
        return hmFeedbackMapper.deleteHmFeedbackById(id);
    }


}
